﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;

namespace WebInvoicingsys.system.storage.stockbill
{
    public partial class stockbill_in_list : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        //传入的参数
        string start_date, end_date, billsno;

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                start_date = Request.Params["start_date"].ToString();
                end_date = Request.Params["end_date"].ToString();
                billsno = Request.Params["billsno"].ToString();
            }
            catch
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, Errormsg.er_Parampost);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        private DataSet GetData()
        {
            //取得传入的参数
            if (GetParma() == false)
            {
                return null;
            }

            string error_msg = "";

            //构造SQL语句
            string sql = @"select  a.APPID,
                                   a.billsno,
                                   a.billdate,
                                   (select purchase.arrivaldate from purchase where purchase.pcsid=a.APPID) arrivaldate,
                                   (select supplier.suppliername from purchase, supplier where purchase.pcsid=a.APPID and purchase.supplierid=supplier.supplierid) suppliername,
	                               (select purchase.phone from purchase where purchase.pcsid=a.APPID) phone,
	                               (select purchase.fax from purchase where purchase.pcsid=a.APPID) fax,
	                               a.sysdate,
	                               (select sysuser.username from sysuser where sysuser.userid=a.userid) username,
	                               a.remark,
	                               a.checkstu,
	                               (select sysuser.username from sysuser where sysuser.userid=a.checkuserid) checkusername,
	                               a.Checkdate,
	                               a.checkremark,
	                               a.m_stu,
	                               count(distinct c.goodsclassid) qty1,
	                               sum(b.Qty1) qty2
                              from bills a, billsdetail b, v_product c
                               where a.billsno=b.billsno
                                 and b.productcode=c.productcode 
                                 and a.Billtype='1003'";

            sql += (start_date == "") ? "" : String.Format(" and a.billdate >= '{0}'", start_date);
            sql += (end_date == "") ? "" : String.Format(" and a.billdate <= '{0}'", end_date);
            sql += (billsno == "") ? "" : String.Format(" and a.billsno like '{0}%'", billsno);

            sql += " group by a.APPID,a.billsno,a.billdate,a.sysdate, a.remark, a.checkstu, a.userid, a.Checkdate, a.checkremark, a.m_stu, a.checkuserid";


            sql = String.Format(sql, Session["userid"].ToString());

            Execute exec = new Execute(Session["connectionString"].ToString());

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (ds == null)
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return null;
            }

            DataSet dsResult = new DataSet();

            DataTable dt = new DataTable("List");

            dt.Columns.Add("dgvID");
            dt.Columns.Add("pcsid");
            dt.Columns.Add("billsno");
            dt.Columns.Add("billdate");
            dt.Columns.Add("arrivaldate");
            dt.Columns.Add("suppliername");
            dt.Columns.Add("phone");
            dt.Columns.Add("fax");
            dt.Columns.Add("sysdate");
            dt.Columns.Add("username");
            dt.Columns.Add("remark");
            dt.Columns.Add("checkstu");
            dt.Columns.Add("checkusername");
            dt.Columns.Add("checkdate");
            dt.Columns.Add("checkremark");
            dt.Columns.Add("m_stu");
            dt.Columns.Add("qty1");
            dt.Columns.Add("qty2");


            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dt.NewRow();

                drnew["dgvID"] = (dt.Rows.Count + 1).ToString();
                drnew["pcsid"] = dr["APPID"].ToString();
                drnew["billsno"] = dr["billsno"].ToString();
                drnew["billdate"] = dr["billdate"].ToString();
                drnew["arrivaldate"] = dr["arrivaldate"].ToString();
                drnew["suppliername"] = dr["suppliername"].ToString();
                drnew["phone"] = dr["phone"].ToString();
                drnew["fax"] = dr["fax"].ToString();
                drnew["sysdate"] = dr["sysdate"].ToString();
                drnew["username"] = dr["username"].ToString();
                drnew["remark"] = dr["remark"].ToString();
                drnew["checkstu"] = (dr["checkstu"].ToString() == "0" ? "未审" : "已审");
                drnew["checkusername"] = dr["checkusername"].ToString();
                drnew["checkdate"] = dr["checkdate"].ToString();
                drnew["checkremark"] = dr["checkremark"].ToString();
                drnew["m_stu"] = (dr["m_stu"].ToString() == "0" ? "否" : "是");

                drnew["qty1"] = dr["qty1"].ToString();
                drnew["qty2"] = dr["qty2"].ToString();

                dt.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dt);

            return dsResult;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void bind()
        {
            DataSet ds = GetData();

            if (ds == null)
            {
                return;
            }

            rplist.DataSource = ds.Tables[0];
            rplist.DataBind();
        }

        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="id"></param>
        private void Check(string id)
        {
            if (String.IsNullOrEmpty(id))
            {
                return;
            }

            GetParma();

            string url = String.Format("/system/storage/stockbill/stockbill_in_check.aspx?id={0}&start_date={1}&end_date={2}&billsno={3}", id, start_date, end_date, billsno);

            Response.Redirect(url);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //查询
                bind();
            }
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string url = "/system/storage/stockbill/stockbill_in.aspx";

            Response.Redirect(url);
        }

        //导出
        protected void btnExport_Click(object sender, EventArgs e)
        {
            //取得数据
            DataSet ds = GetData();

            if (ds == null)
            {
                return;
            }

            //构造导出表头
            String[] headname = { "序号", "计划单号", "仓库单号", "开单日期", "到货时间", "供应商", "联系电话", "传真", "制单日期", "开单人员", "开单备注", "审批状态", "审批人员", "审批日期", "审批备注", "月结", "总类(合计)", "数量(合计)" };

            //导出
            ExportToFile.ExportToExcel(ds.Tables[0], HttpContext.Current.Response, headname);
        }

        //审批
        protected void rplist_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();

            switch (e.CommandName)
            {
                case "Check":
                    {
                        Check(id);

                        //查询
                        bind();

                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
    }
}